package com.L.yygh.hosp.controller.api;

import com.L.yygh.common.exception.YyghException;
import com.L.yygh.common.result.Result;
import com.L.yygh.common.result.ResultCodeEnum;
import com.L.yygh.helper.HttpRequestHelper;
import com.L.yygh.hosp.service.HospitalService;
import com.L.yygh.hosp.service.HospitalSetService;
import com.L.yygh.model.hosp.Hospital;
import com.L.yygh.util.MD5;
import com.alibaba.excel.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Api(tags = "医院管理api接口")
@RestController
@RequestMapping("/api/hosp")
public class ApiController {

    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private HospitalSetService hospitalSetService;

    //查询医院接口
    @ApiOperation("查询医院接口")
    @PostMapping("/hospital/show")
    public Result<Hospital> getHospital(HttpServletRequest request){
        //获取传递过来医院的信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);
        //获取医院编号
        String hoscode = (String) paramMap.get("hoscode");
        String sign = (String)paramMap.get("sign");
        //2.根据传递过来的医院编号，查询数据库，看签名是否一样
        String signKey = hospitalSetService.getSignKey(hoscode);
        //3.把数据库查询签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(signKey);
        //4.判断签名是否一致
        if(!sign.equals(signKeyMd5)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }else{
            Hospital hospital = hospitalService.getByHoscode(hoscode);
            return Result.ok(hospital);
        }
    }

    //上传医院接口
    @ApiOperation("上传医院接口")
    @PostMapping("/saveHospital")
    public Result saveHosp(HttpServletRequest request){
        //获取传递过来医院的信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);
        //1.获取医院系统中传递过来的签名,这个签名进行了Md5的加密
        String sign = (String)paramMap.get("sign");
        //2.根据传递过来的医院编号，查询数据库，看签名是否一样
        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        //3.把数据库查询签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(signKey);
        //4.判断签名是否一致
        if(!sign.equals(signKeyMd5)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }else{
            //参数过程中"+"转换为了" "，因此我们需要转换回来
            String logoData = (String) paramMap.get("logoData");
            if(!StringUtils.isEmpty(logoData)){
                logoData = logoData.replaceAll(" ","+");
                paramMap.put("logoData",logoData);
            }

            //调用service的方法
            hospitalService.save(paramMap);
            return Result.ok();
        }

    }
}
